package cn.pegasus.teamwork.appinterface.util;

/**
 * Created by Administrator on 2017/8/24 0024.
 */
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class XMLUtil {
    /**
     * 解析xml,返回第一级元素键值对。如果第一级元素有子节点，则此节点的值是子节点的xml数据。
     * @param strxml
     * @return
     * @throws JDOMException
     * @throws IOException
     */
    public static Map doXMLParse(String strxml) throws JDOMException, IOException {
        strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");

        if(null == strxml || "".equals(strxml)) {
            return null;
        }

        Map m = new HashMap();

        InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(in);
        Element root = doc.getRootElement();
        List list = root.getChildren();
        Iterator it = list.iterator();
        while(it.hasNext()) {
            Element e = (Element) it.next();
            String k = e.getName();
            String v = "";
            List children = e.getChildren();
            if(children.isEmpty()) {
                v = e.getTextNormalize();
            } else {
                v = XMLUtil.getChildrenText(children);
            }

            m.put(k, v);
        }

        //关闭流
        in.close();

        return m;
    }

    /**
     * @author
     * @date
     * @Description：返回给微信的参数
     * @param return_code 返回编码
     * @param return_msg  返回信息
     * @return
     */
    public static String setXML(String return_code, String return_msg) {
        return "<xml><return_code><![CDATA[" + return_code
                + "]]></return_code><return_msg><![CDATA[" + return_msg
                + "]]></return_msg></xml>";
    }
    
    /**
     * 获取子结点的xml
     * @param children
     * @return String
     */
    public static String getChildrenText(List children) {
        StringBuffer sb = new StringBuffer();
        if(!children.isEmpty()) {
            Iterator it = children.iterator();
            while(it.hasNext()) {
                Element e = (Element) it.next();
                String name = e.getName();
                String value = e.getTextNormalize();
                List list = e.getChildren();
                sb.append("<" + name + ">");
                if(!list.isEmpty()) {
                    sb.append(XMLUtil.getChildrenText(list));
                }
                sb.append(value);
                sb.append("</" + name + ">");
            }
        }

        return sb.toString();
    }

    public static void main(String[] args) throws JDOMException, IOException {
		String result = "<xml><appid><![CDATA[wx620cf6b8dddd4fb8]]></appid>"+
				"<bank_type><![CDATA[CFT]]></bank_type>"+
				"<cash_fee><![CDATA[1]]></cash_fee>"+
				"<device_info><![CDATA[WEB]]></device_info>"+
				"<fee_type><![CDATA[CNY]]></fee_type>"+
				"<is_subscribe><![CDATA[Y]]></is_subscribe>"+
				"<mch_id><![CDATA[1494181972]]></mch_id>"+
				"<nonce_str><![CDATA[wl8DTCRBBUu9OuhjHcKaMncBNjm7qZBB]]></nonce_str>"+
				"<openid><![CDATA[o8jJF06eSBT1Rqp29ONQct0DMB0o]]></openid>"+
				"<out_trade_no><![CDATA[405920757294563328]]></out_trade_no>"+
				"<result_code><![CDATA[SUCCESS]]></result_code>"+
				"<return_code><![CDATA[SUCCESS]]></return_code>"+
				"<sign><![CDATA[E16534677549F888AA29E727D9495B95]]></sign>"+
				"<time_end><![CDATA[20180125030418]]></time_end>"+
				"<total_fee>1</total_fee>"+
				"<trade_type><![CDATA[JSAPI]]></trade_type>"+
				"<transaction_id><![CDATA[4200000087201801250178564417]]></transaction_id>"+
				"</xml>" ;

		Map<String, Object> resultMap = doXMLParse(result);
		System.out.println(resultMap.get("transaction_id"));
		
	}
}